//
//  XiaoJiaDiaoChaViewController.m
//  tangshantongjiAPPFull
//
//  Created by yuntai on 2018/4/25.
//  Copyright © 2018年 yuntai. All rights reserved.
//

#import "XiaoJiaDiaoChaViewController.h"
#import "FullMainCollectionViewCell.h"
#import "UICollectionView+HD_NoList.h"
#import "CommonModel.h"
#import "ZaiXianDiaoChaViewController.h"

@interface XiaoJiaDiaoChaViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
@property (nonatomic,strong)UICollectionView *collectionview;
@property (nonatomic, strong) NSMutableArray *array;

@end
static NSString *fullMainCollectionCellID = @"FullMainCollectionViewCell";

@implementation XiaoJiaDiaoChaViewController
-(NSMutableArray *)array{
    if (_array == nil) {
        _array = [NSMutableArray array];
    }
    return _array;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenBoundWidth,   kNavigationHeight-44)];
    topView.backgroundColor = kNavigationColor;
    [self.view addSubview:topView];
    self.titleLabel.text = @"销价调查";
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((kScreenBoundWidth-60)/2,(kScreenBoundWidth-60)/2); ;
    layout.minimumInteritemSpacing = 20;
    layout.minimumLineSpacing = 20;
    [layout setSectionInset:UIEdgeInsetsMake(20, 20, 20, 20)];
    
    self.collectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kNavigationHeight, kScreenBoundWidth, kScreenBoundHeight-kNavigationHeight) collectionViewLayout:layout];
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    self.collectionview.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.collectionview];
    
    [self.collectionview registerNib:[UINib nibWithNibName:fullMainCollectionCellID bundle:nil] forCellWithReuseIdentifier:fullMainCollectionCellID];
    
    BOOL ishow = self.array.count ? NO : YES;
    [self.collectionview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
    [self.collectionview reloadData];

    weakself(self)
    self.collectionview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakself.currentPage = 1;
        [weakself getdata];
    }];
    [self.collectionview.mj_header beginRefreshing];
    
    self.collectionview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakself getdata];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //    self.navigationController.navigationBar.hidden = YES;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (IBAction)backBtnAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count;
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    FullMainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:fullMainCollectionCellID forIndexPath:indexPath];
    
    CommonModel *model = self.array[indexPath.row];
    cell.nameLabel.hidden = YES;
    cell.subNameLabel.hidden = NO;
    cell.nameLabelheight.constant = 0;
   
    cell.subNameLabel.text = model.name?:@"";
    [cell.imageIconView sd_setImageWithURL:[NSURL URLWithString:model.background] placeholderImage:[UIImage imageNamed:@"ic_default_img"]];
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CommonModel *md = self.array[indexPath.row];
    ZaiXianDiaoChaViewController *VC =[[ZaiXianDiaoChaViewController alloc]init];
    VC.type = XiaoJiaDiaoCha;
    VC.modelid = md.modelId;
    VC.title = md.name;
    [self.navigationController pushViewController:VC animated:YES];
}



- (void)getdata{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:kTokenID forKey:@"token"];
    [params setValue:@(self.currentPage) forKey:@"page"];
    
    [NetWork PostNetWorkWithUrl:@"api/cp/getCpMenu.ajax" with:params successBlock:^(NSDictionary *dic) {
        [self.collectionview.mj_header endRefreshing];
        [self.collectionview.mj_footer endRefreshing];
        
        NSArray *temparray = dic[@"data"][@"data"];
        if (self.currentPage == 1) {
            [self.array removeAllObjects];
        }
        if (temparray.count<4) {
            [self.collectionview.mj_footer endRefreshingWithNoMoreData];
        }else{
            self.currentPage ++;
            [self.collectionview.mj_footer resetNoMoreData];
        }
        
        NSString *bgurl = dic[@"data"][@"url"];
        [self.bgImageView sd_setImageWithURL:[NSURL URLWithString:bgurl?:@""] placeholderImage:[UIImage imageNamed:@"ic_default_img"]];
    
        
        [self.array addObjectsFromArray:[CommonModel mj_objectArrayWithKeyValuesArray:temparray]];
        BOOL ishow = self.array.count ? NO : YES;
        [self.collectionview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        [self.collectionview reloadData];
        
    } FailureBlock:^(NSString *msg) {
        [self.collectionview.mj_header endRefreshing];
        [self.collectionview.mj_footer endRefreshing];
        [HUDManager showWarningWithText:msg];
        BOOL ishow = self.array.count ? NO : YES;
        [self.collectionview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        
        
    } errorBlock:^(id error) {
        [self.collectionview.mj_header endRefreshing];
        [self.collectionview.mj_footer endRefreshing];
        BOOL ishow = self.array.count ? NO : YES;
        [self.collectionview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        
        
    }];
    
}
@end
